package com.cheifsteam.springframework.context.support;

import com.cheifsteam.springframework.beans.BeansException;
import com.cheifsteam.springframework.beans.factory.ConfigurableListableBeanFactory;
import com.cheifsteam.springframework.beans.factory.config.BeanFactoryPostProcessor;
import com.cheifsteam.springframework.beans.factory.config.BeanPostProcessor;
import com.cheifsteam.springframework.context.ApplicationContext;
import com.cheifsteam.springframework.context.ConfigurableApplicationContext;
import com.cheifsteam.springframework.core.io.DefaultResourceLoader;

import java.util.Map;

/**
 * @author 屈燃希
 * @date 2025/1/30 15:49
 * @content
 */
public abstract class AbstractApplicationContext extends DefaultResourceLoader implements ConfigurableApplicationContext {
    @Override
    public void refresh() throws BeansException {

        // 1. 创建 BeanFactory，并加载 BeanDefinition
        refreshBeanFactory();
        //2.获取BeanFactory
        ConfigurableListableBeanFactory beanFactory = getBeanFactory();

        // 3. 在 Bean 实例化之前，执行 BeanFactoryPostProcessor (Invoke factory processors registered as beans in the context.)
        invokeBeanFactoryPostProcessors(beanFactory);
        // 4. 注册 BeanPostProcessor (Invoke factory processors registered as beans in the context.)
        registerBeanPostProcessors(beanFactory);
        // 5. 实例化所有剩余的非懒加载的单例
        beanFactory.preInstantiateSingletons();


    }


    private void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
        Map<String, BeanFactoryPostProcessor> beanFactoryPostProcessorMap = beanFactory.getBeansOfType(BeanFactoryPostProcessor.class);
        for (BeanFactoryPostProcessor beanFactoryPostProcessor : beanFactoryPostProcessorMap.values()) {
            beanFactoryPostProcessor.postProcessBeanFactory(beanFactory);
        }
    }

    private void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
        Map<String, BeanPostProcessor> beanPostProcessorMap = beanFactory.getBeansOfType(BeanPostProcessor.class);
        for (BeanPostProcessor beanPostProcessor : beanPostProcessorMap.values()) {
            beanFactory.addBeanPostProcessor(beanPostProcessor);
        }
    }
    protected abstract void  refreshBeanFactory() throws  BeansException;


    protected abstract ConfigurableListableBeanFactory getBeanFactory();
    @Override
    public Object getBean(String name) {
        return getBeanFactory().getBean(name);
    }

    @Override
    public Object getBean(String name, Object... args) {
        return getBeanFactory().getBean(name,args);
    }


    @Override
    public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
        return getBeanFactory().getBean(name,requiredType);
    }

    @Override
    public <T> Map<String, T> getBeansOfType(Class<T> requiredType) throws BeansException {
        return getBeanFactory().getBeansOfType(requiredType);
    }

    @Override
    public String[] getBeanDefinitionNames() {
        return getBeanFactory().getBeanDefinitionNames();
    }
}
